package cn.javaxc.mvcwork.util.io;
import cn.javaxc.mvcwork.util.common.CommonUtil;
import cn.javaxc.mvcwork.util.common.*;
import cn.javaxc.mvcwork.util.io.FileUtils;
import cn.javaxc.mvcwork.controller.MVCConfig;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import org.springframework.web.multipart.*;
import org.springframework.util.*;
import cn.javaxc.mvcwork.modules.file.FileInfo;


public abstract class HttpMultiPartParser {
  protected final String fs = System.getProperty("file.separator");
  protected final int ONE_MB = 1024 * 1024 * 1;
  protected boolean useUniqueName = false;
  protected boolean usePathParam = false;
  protected boolean useDateForFolder = false;
  protected double version = -1;
  protected Object extensionId;
  protected long sizeMax=CommonUtil.parseLong(MVCConfig.getConfig().getClassProperty(this.getClass(),"sizeMax"),-1);

  protected MultiValueMap<String, org.springframework.web.multipart.MultipartFile> multipartFiles= new LinkedMultiValueMap();
  protected Map<String, String[]> multipartParameters=new HashMap();
  protected Map<String, String> multipartParameterContentTypes= new HashMap();
  public void setUseUniqueName(boolean tmp) {
    this.useUniqueName = tmp;
  }


  
  public void setUsePathParam(boolean tmp) {
    this.usePathParam = tmp;
  }


  
  public void setUseDateForFolder(boolean tmp) {
    this.useDateForFolder = tmp;
  }


  
  public void setVersion(double tmp) {
    this.version = tmp;
  }


  
  public void setExtensionId(Object tmp) {
    this.extensionId = tmp;
  }


  
  public boolean getUseUniqueName() {
    return useUniqueName;
  }


  
  public boolean getUsePathParam() {
    return usePathParam;
  }


  
  public boolean getUseDateForFolder() {
    return useDateForFolder;
  }


  
  public double getVersion() {
    return version;
  }


  
  public Object getExtensionId() {
    return extensionId;
  }

    public long getSizeMax(){
        return sizeMax;
    }
    public void setSizeMax(long sizeMax){
        this.sizeMax = sizeMax;
    }
  
  public void parseData(HttpServletRequest request, String saveInDir)
      throws Exception {
    processData(request, saveInDir);
  }


  
  public void parseData(HttpServletRequest request)
      throws Exception {
    processData(request, null);
  }


  
  protected abstract void processData(HttpServletRequest request, String saveInDir)
      throws Exception ;

  public Map<String, FileInfo> getFileMap()
  {
	return (Map)multipartFiles.toSingleValueMap();
  }  
	public MultiValueMap<String, MultipartFile> getMultipartFiles() {
	  return this.multipartFiles;
	}
  public Map<String, String[]> getMultipartParameters()
  {
    return this.multipartParameters;
  }

  public Map<String, String> getMultipartParameterContentTypes()
  {
    return this.multipartParameterContentTypes;
  }
}
